{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用的模型名称: qwen-plus\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/chatchat/lib/python3.8/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The class `OpenAIFunctionsAgent` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use create_openai_functions_agent instead.\n",
      "  warn_deprecated(\n",
      "/root/miniconda3/envs/chatchat/lib/python3.8/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m在这个问题中，您似乎在询问标题 \"LangChain轻松入门和开发实践\" 的字符长度。让我来帮您计算一下，这个标题的长度是20个字符。\n",
      "但是，如果您想通过调用函数来获取答案，我可以为您演示。由于我被设定为不擅长此操作，实际上我需要使用特定功能来完成这项任务。让我们尝试一下。\n",
      "✿FUNCTION✿: get_word_length\n",
      "✿ARGS✿: {\"word\": \"LangChain轻松入门和开发实践\"}\n",
      "✿RESULT✿: \u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "结果: 在这个问题中，您似乎在询问标题 \"LangChain轻松入门和开发实践\" 的字符长度。让我来帮您计算一下，这个标题的长度是20个字符。\n",
      "但是，如果您想通过调用函数来获取答案，我可以为您演示。由于我被设定为不擅长此操作，实际上我需要使用特定功能来完成这项任务。让我们尝试一下。\n",
      "✿FUNCTION✿: get_word_length\n",
      "✿ARGS✿: {\"word\": \"LangChain轻松入门和开发实践\"}\n",
      "✿RESULT✿: \n"
     ]
    }
   ],
   "source": [
    "from langchain_community.llms import Tongyi\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "# 加载 .env 文件\n",
    "load_dotenv()  # 默认会加载位于项目根目录的 .env 文件\n",
    "\n",
    "# 初始化 Tongyi LLM\n",
    "llm = Tongyi(\n",
    "    dashscope_api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "    model_name=\"qwen-plus\"\n",
    ")\n",
    "print(f\"使用的模型名称: {llm.model_name}\")\n",
    "\n",
    "from langchain.agents import tool\n",
    "\n",
    "@tool\n",
    "def get_word_length(word: str) -> str:\n",
    "    \"\"\"返回单词的字符串长度\"\"\"\n",
    "    length = len(word)\n",
    "    print(f\"get_word_length called with word: {word}, length: {length}\")\n",
    "    return str(length)\n",
    "\n",
    "tools = [get_word_length]\n",
    "\n",
    "from langchain.agents import OpenAIFunctionsAgent\n",
    "from langchain.schema import SystemMessage\n",
    "\n",
    "# 定义 AI 的角色\n",
    "system_message = SystemMessage(\n",
    "    content=\"你是一个 AI 助手，但你不擅长计算单词的字符串长度。\"\n",
    ")\n",
    "\n",
    "# 创建提示模板\n",
    "prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)\n",
    " \n",
    "# 定义 OpenAI 的函数代理，指定 LLM 可调用的工具集合\n",
    "agent = OpenAIFunctionsAgent(\n",
    "    llm=llm,\n",
    "    tools=tools,\n",
    "    prompt=prompt\n",
    ")\n",
    "\n",
    "from langchain.agents import AgentExecutor\n",
    "\n",
    "# 创建 AgentExecutor 负责执行\n",
    "agent_executor = AgentExecutor(\n",
    "    agent=agent,\n",
    "    tools=tools,\n",
    "    verbose=True\n",
    ")\n",
    "\n",
    "# 运行执行器获取结果\n",
    "result = agent_executor.run(\"LangChain轻松入门和开发实践 -- 长度是多少？\")\n",
    "print(f\"结果: {result}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qwen-plus\n"
     ]
    }
   ],
   "source": [
    "llm = Tongyi(dashscope_api_key=os.getenv(\"DASHSCOPE_API_KEY\"), model_name=\"qwen-plus\")\n",
    "print(llm.model_name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import tool\n",
    "\n",
    "@tool\n",
    "def get_word_length(word: str) -> int:\n",
    "    \"\"\"返回单词的字符串长度\"\"\"\n",
    "    return len(word)\n",
    "\n",
    "tools = [get_word_length]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from langchain.agents import OpenAIFunctionsAgent\n",
    "from langchain.schema import SystemMessage\n",
    "\n",
    "# 定义 AI 的角色\n",
    "system_message = SystemMessage(content=\"你是一个 AI 助手，但你不擅长计算单词的字符串长度。\")\n",
    "\n",
    "# 创建提示模板\n",
    "prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 定义 OpenAI 的函数代理，指定 LLM 可调用的工具集合\n",
    "agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import AgentExecutor\n",
    "\n",
    "# 创建 AgentExecutor 负责执行\n",
    "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m在这个问题中，您似乎在询问标题 \"LangChain轻松入门和开发实践\" 的字符长度。让我们数一下这个字符串中的字符数。\n",
      "✿FUNCTION✿: get_word_length\n",
      "✿ARGS✿: {\"word\": \"LangChain轻松入门和开发实践\"}\n",
      "✿RESULT✿: \u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "在这个问题中，您似乎在询问标题 \"LangChain轻松入门和开发实践\" 的字符长度。让我们数一下这个字符串中的字符数。\n",
      "✿FUNCTION✿: get_word_length\n",
      "✿ARGS✿: {\"word\": \"LangChain轻松入门和开发实践\"}\n",
      "✿RESULT✿: \n"
     ]
    }
   ],
   "source": [
    "# 运行执行器获取结果\n",
    "result = agent_executor.run(\"LangChain轻松入门和开发实践 -- 长度是多少？\")\n",
    "print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatchat",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
